<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="职务级别名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="levelName">
              <a-input :disabled="formDisabled" v-model="model.levelName" placeholder="请输入职务级别名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职务级别代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="levelCode">
              <a-input :disabled="formDisabled" v-model="model.levelCode" placeholder="请输入职务级别代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职务级别序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="levelIndex">
              <a-input-number
                :disabled="formDisabled"
                :max="99999999"
                :min="0"
                v-model="model.levelIndex"
                placeholder="请输入职务级别序号"
                style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.status" dictCode="ENABLE_OR_DISABLE"
                                 placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea :disabled="formDisabled" v-model="model.description" rows="4" placeholder="请输入描述"/>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">
                      <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditStatus">
                        <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.auditStatus" dictCode="audit_status" placeholder="请选择审核状态" />
                      </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                      <a-form-model-item label="是否已删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">
                        <j-dict-select-tag :disabled=formDisabled type="radio" v-model="model.delFlag" dictCode="del_flag" placeholder="请选择是否已删除" />
                      </a-form-model-item>
                    </a-col>-->
          <!--          <a-col :span="12">
                      <a-form-model-item label="经办人编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerId">
                        <a-input :disabled=formDisabled v-model="model.handlerId" placeholder="请输入经办人编号"  ></a-input>
                      </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                      <a-form-model-item label="经办人部门ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerDeptId">
                        <a-input :disabled=formDisabled v-model="model.handlerDeptId" placeholder="请输入经办人部门ID"  ></a-input>
                      </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                      <a-form-model-item label="经办人部门编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="handlerDeptCode">
                        <a-input :disabled=formDisabled v-model="model.handlerDeptCode" placeholder="请输入经办人部门编码"  ></a-input>
                      </a-form-model-item>
                    </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
  name: 'ErpOrgPositionLevelForm',
  components: {},
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        levelName: [{ required: true, message: '请输入职务级别名称!' }],
        levelCode: [{ required: true, message: '请输入职务级别代码!' }],
        positionCategory: [{ required: true, message: '请选择归属序列!' }],
        levelIndex: [
          { required: true, message: '请输入职务级别序号!' },
          { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
        ]
      },
      url: {
        add: '/api/erp/org/positionlevel/erpOrgPositionLevel/add',
        edit: '/api/erp/org/positionlevel/erpOrgPositionLevel/edit',
        queryById: '/api/erp/org/positionlevel/erpOrgPositionLevel/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    }
  }
}
</script>
